package com.shiliao.app.activity.index.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.shiliao.app.R;
import com.shiliao.app.activity.index.entity.WaitExamineEntity;
import com.shiliao.app.activity.index.presenter.WaitExamineDetailPresenter;
import com.shiliao.app.activity.index.presenter.WaitExaminePresenter;
import com.shiliao.app.activity.index.view.WaitExaminDetailView;
import com.shiliao.app.event.MessageEvent;
import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.utils.WebSetUtil;
import com.shiliao.app.utils.YMServerUtil;
import com.shiliao.app.weight.ActionBarView;
import com.shiliao.app.weight.ProgressWebView;
import com.shiliao.app.weight.dialog.ExamineDialog;
import com.shiliao.app.weight.dialog.NewExamineDialog;
import com.yimei.framework.base.ui.BaseActivity;
import com.yimei.framework.util.LogUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yinzhiqiang on 2018/4/22.
 */

public class WaitExaminDetailActivity extends BaseActivity implements WaitExaminDetailView{

    @BindView(R.id.webview)
    ProgressWebView webview;

    @BindView(R.id.title_bar)
    ActionBarView mActionBar;

    @BindView(R.id.tv_Examine)
    TextView mExamine;

    @BindView(R.id.tv_share_friend)
    TextView shareFriend;
    private InviteFriendDialog inviteFriendDialog;
    private WaitExamineDetailPresenter waitExamineDetailPresenter;
    private String id;
    private String type;
    private String status;
    private String title;
    private String userId;

    private String shareTitle;//分享的title
    private String sharePicUrl;//分享的图标
    private String shareArticleUrl;//分享的链接
    private String shareDescription;//分享的描述

    private List<WaitExamineEntity> list;
    @Override
    public int getLayoutId() {
        return R.layout.activity_wait_examie_detail;
    }

    @Override
    public void initView() {
        super.initView();
        mActionBar.setTitles("待审核详情");
        mActionBar.setBackClick(this);
        mActionBar.getBackView().setVisibility(View.VISIBLE);
        waitExamineDetailPresenter=new WaitExamineDetailPresenter(this);

        Intent intent=getIntent();
         id=intent.getStringExtra("id");
         type=intent.getStringExtra("type");
         status=intent.getStringExtra("status");
         title=intent.getStringExtra("title");
         userId=YMUserManager.getUserId(this);
         waitExamineDetailPresenter.requestShare(id,userId);
         waitExamineDetailPresenter.getAuditResult();
//        new InviteFriendDialog(NewsDetailActivity.this,
//                shareArticleUrl, shareTitle, sharePicUrl, shareDescription, userId,
//                "article", article_id);

        LogUtil.e("yzq","id:"+id+" type:"+type+" status:"+status);
        if (!TextUtils.isEmpty(status)&&status.equals("0")){
            mExamine.setText("审核");
        }else if (!TextUtils.isEmpty(status)&&status.equals("1")){
            mExamine.setText("重新审核");
        }
        waitExamineDetailPresenter.requestExaminDeatil(userId,id,type);
        WebSetUtil.setWebView(webview);
    }

    @OnClick(R.id.tv_Examine)
    public void Examine(){
//        ExamineDialog dialog=new ExamineDialog(this);
//        dialog.show(false);
//
//        dialog.commit(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (dialog.getRadio()==0){
//                    showToast("您还没选择");
//                    return;
//                }
//                LogUtil.e("yzq",dialog.getRadio()+"==");
//              waitExamineDetailPresenter.requestAuditResult(userId,id,type,dialog.getRadio());
//              dialog.dismiss();
//            }
//        });
        NewExamineDialog dialog=new NewExamineDialog(this,list);
        dialog.show(false);
        dialog.commit(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.e("yzq",dialog.getResult()+"==");
                waitExamineDetailPresenter.requestAuditResult(userId,id,type,dialog.getResult());
              dialog.dismiss();
            }
        });
    }

    @OnClick(R.id.tv_share_friend)
    public void share(){
        inviteFriendDialog=new InviteFriendDialog(this,
                shareArticleUrl,shareTitle,sharePicUrl,shareDescription,
                userId,type,id);
        inviteFriendDialog.show();
    }

    @Override
    public void getContentText(String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webview.loadDataWithBaseURL(YMServerUtil.BASE_URL,
                        text, "text/html", "utf-8", null);
            }
        });
    }

    @Override
    public void sendBordCast() {
        MessageEvent event = new MessageEvent();
        event.eventName = MessageEvent.EVENT_UPDATE_WAIT_EXAMINE_LIST;
        EventBus.getDefault().post(event);
    }

    @Override
    public void commitSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast("提交成功`");
                mExamine.setText("重新审核");
            }
        });

    }

    @Override
    public void shareContent(final String title, final String picUrl, final String articleUrl, final String description) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                shareTitle = title;
                shareArticleUrl = articleUrl;
                shareDescription = description;
                sharePicUrl = picUrl;
            }
        });
    }

    @Override
    public void getExaminResultList(List<WaitExamineEntity> list) {
        this.list=list;
    }
}
